package yxy.game.pm2.bean.cluster;

import org.redisson.api.RMap;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.BaseGameRedisBean;

import java.util.ArrayList;
import java.util.Collection;

//专服，比"区"更高一级，一个专服有多个区；官方专服、混服、渠道专服等
//专服-》区-》服
public class ClusterBean extends BaseGameRedisBean {
	// bean===========================================================================

	public static int defaultClusterId = 1; //默认专服id

	private int clusterId;
	private ArrayList<String> areaList = new ArrayList<>();
	private boolean meraged;// 是否已被合并
	private String name; //专服名字

	public int getClusterId() {
		return clusterId;
	}

	public void setClusterId(int clusterId) {
		this.clusterId = clusterId;
	}

	public ArrayList<String> getAreaList() {
		return areaList;
	}

	public void setAreaList(ArrayList<String> areaList) {
		this.areaList = areaList;
	}

	public boolean isMeraged() {
		return meraged;
	}

	public void setMeraged(boolean meraged) {
		this.meraged = meraged;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// func==========================================================================

	public  void addArea(int areaId) {
		try {
			if (tryLock()) {
				AreaBean areaBean = AreaBean.get(areaId);
				if (areaBean == null) {
					return;
				}
				int oldClusterId = areaBean.getClusterId();
				if (oldClusterId == getClusterId()) {
					return;
				}
				ClusterBean oldClusterBean = ClusterBean.get(oldClusterId);
				if (oldClusterBean != null) {
					oldClusterBean.removeArea(areaId);
				}
				areaBean.setClusterId(getClusterId());
				areaBean.set();
				String areaCode = String.valueOf(areaId);
				if (getAreaList().contains(areaCode)) {
					return;
				}
				getAreaList().add(areaCode);
				set();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	public void removeArea(int areaId) {
		try {
			if (tryLock()) {
				AreaBean areaBean = AreaBean.get(areaId);
				if (areaBean == null) {
					return;
				}
				int oldClusterId = areaBean.getClusterId();
				if (oldClusterId == getClusterId()) {
					areaBean.setClusterId(0);
					areaBean.set();
				}
				String areaCode = String.valueOf(areaId);
				if (!getAreaList().contains(areaCode)) {
					return;
				}
				getAreaList().remove(areaCode);
				set();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			unlock();
		}
	}

	// redis==========================================================================

	static public String key() {
		return String.format("CLUSTER:MAP");
	}

	static private String clusterKey(int clusterId) {
		return String.format("cluster_%s", clusterId);
	}

	static private RMap<String, ClusterBean> touch() throws RedisException {
		RedisConnection redis = RedisManager.getInstance().getConnection(RedisType.REDIS_GAME);
		return RedisManager.client(redis).getMap(key());
	}

	static public Collection<ClusterBean> all() {
		try {
			return touch().readAllValues();
		} catch (RedisException e) {
		}
		return new ArrayList<>();
	}

	static public ClusterBean get(int clusterId) {
		try {
			RMap<String, ClusterBean> map = touch();
			ClusterBean clusterBean = map.get(clusterKey(clusterId));
			return clusterBean;
		} catch (RedisException e) {
			e.printStackTrace();
			return null;
		}
	}

	/** 生成一个新专服 */
	static public ClusterBean create(int clusterId) throws RedisException {
		ClusterBean groupBean = new ClusterBean();
		groupBean.setClusterId(clusterId);
		groupBean.set();
		return groupBean;
	}

	public void set() {
		try {
			RMap<String, ClusterBean> map = touch();
			map.put(clusterKey(getClusterId()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String lockKey() {
		return String.format("lock:%s", key());
	}

}
